package cn.lijigou.shopping;

import java.util.ArrayList;

import cn.lijigou.shopping.domain.Order;
import cn.lijigou.shopping.service.MSystem;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.Html;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

/**
 * @author 赵奇 E-mail:cagezhao@gmail.com
 * @version 创建时间：2012-1-14 上午10:23:44 类说明
 */

public class CompleteOrderActivity extends Activity implements OnClickListener {

	private String shop_title;
	private String phone_num;
	private TextView mShopTextView;
	private Button mShowOrder_Btn;
	private  SharedPreferences mSharedPreference = null;
	private Button show_buy = null;
	private TextView text_center = null;
	public static Boolean isAnima=false;
	private Handler waitHandler = new Handler(){

		
		public void handleMessage(Message msg) {
			// TODO Auto-generated method stub
			super.handleMessage(msg);
			if(msg.what==0){
				OrdersActivity.mButton_paid.setChecked(true);
				OrdersActivity.radioButtonId = 1;
					
				OrdersActivity.pages[0] = 0;
				OrdersActivity.pages[1] = 0;
				OrdersActivity.hasNextPages[0] = true;
				OrdersActivity.hasNextPages[1] = true;
				OrdersActivity.mPayedOrders.clear();
				OrdersActivity.mUnPayOrders.clear();
				OrdersActivity.mAdapter.setDate(new ArrayList<Order>());
				OrdersActivity.mAdapter.notifyDataSetChanged();
			}
		}
		
	};
	
	
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.complete_order);

		show_buy = (Button) this.findViewById(R.id.show_buy);
		show_buy.setOnClickListener(this);

		text_center = (TextView)this.findViewById(R.id.text_center);
		text_center.setText("完成订单");
		findViewById(R.id.titleLeftView).setVisibility(View.GONE);
		findViewById(R.id.titleRightView).setVisibility(View.GONE);
		if (mSharedPreference == null) {
			mSharedPreference = this.getSharedPreferences("user",
					Context.MODE_PRIVATE);
		}
		phone_num = mSharedPreference.getString("phoneNum", "");

		Intent mIntent = this.getIntent();
		shop_title = mIntent.getStringExtra("shopTitle");
		
		
		
		mShopTextView = (TextView) findViewById(R.id.shop_title);
		mShopTextView.setText(shop_title  );
		mShopTextView.setText(Html.fromHtml(shop_title+"<br><br>"+"*确认短信将会发送到手机号："
				+"<font color=#6e9ecf>"+phone_num+"</font> "
				+"请凭短信与商家预约消费")
				);
		mShowOrder_Btn = (Button) findViewById(R.id.show_order);
		mShowOrder_Btn.setOnClickListener(this);
	}

	
	public void onClick(View v) {
		MSystem.pv +=1;
		switch (v.getId()) {
		case R.id.show_buy:
			showHome();
			break;
		case R.id.show_order:
			showOrders();
			break;
		default:
			break;
		}
	}

	private void showHome() {
		if(HomeActivity.parentMainTab!=null){
			HomeActivity.parentMainTab.mHost.setCurrentTabByTag("home_tab");
			HomeActivity.parentMainTab.radioButtons.get(0).setChecked(true);
		}else{
			Intent mIntent  = new Intent(CompleteOrderActivity.this,MainTabActivity.class);
		    startActivity(mIntent);
		    CompleteOrderActivity.this.finish();
			
		}
		
		
		for (int i = 0; i < ShopDetailActivity.mList.size(); i++) {
			ShopDetailActivity.mList.get(i).finish();
		}
		
		CompleteOrderActivity.this.finish();
	}

	private void showOrders() {
		
		waitHandler.sendEmptyMessage(0);
		isAnima=true;
		
		if(HomeActivity.parentMainTab!=null){
			HomeActivity.parentMainTab.mHost.setCurrentTabByTag("order_tab");
			HomeActivity.parentMainTab.radioButtons.get(3).setChecked(true);
		}else{
			Intent mIntent  = new Intent(CompleteOrderActivity.this,MainTabActivity.class);
			mIntent.putExtra("order", "ok");
		    startActivity(mIntent);
		    CompleteOrderActivity.this.finish();
			
//		    HomeActivity.parentMainTab.mHost.setCurrentTabByTag("order_tab");
//			HomeActivity.parentMainTab.radioButtons.get(3).setChecked(true);
		}
		
		HomeActivity.parentMainTab.mHost.setCurrentTabByTag("order_tab");
		HomeActivity.parentMainTab.radioButtons.get(3).setChecked(true);
		for (int i = 0; i < ShopDetailActivity.mList.size(); i++) {
			ShopDetailActivity.mList.get(i).finish();
		}
		ShopDetailActivity.mList.clear();
		
		CompleteOrderActivity.this.finish();
	}
}
